package com.chance.cc.crawler.prod.command.job.domain.vm.xcar.realtime;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.proxy.Proxy;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.queue.crawler.CrawlerQueueConfig;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.vm.xcar.XCarAutoCommonCrawlerSchedulerJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
** 爱卡车系页文章信息采集
 **
 */
public class XcarSeriesArticleRealtimeCrawlerScheduleJob extends XCarAutoCommonCrawlerSchedulerJob {

    public static String site = "SN";

    public static void main(String[] args) {
        publishXcarNewsSchedulerJob();
    }

    public static CrawlerJob publishXcarNewsSchedulerJob(){
        CrawlerJob crawlerJob = xcarCwalerJob();



        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("爱卡汽车车系文章每天1点定时采集");
        crawlerScheduleJob.setCrawlerKey(crawlerJob.generateCrawlerKey());
        HttpPage page = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布作业：" + page.getRawText());

        return crawlerJob;
    }

    /**
     * taobao采集
     * @return
     */
    private static Proxy proxy = new Proxy();
    static{
        proxy.setHost("http-dyn.abuyun.com");
        proxy.setPort(9020);
        proxy.setUsername("H7307T4706B25G4D");
        proxy.setPassword("05B4877CC39192C0");

    }
    public static CrawlerJob xcarCwalerJob() {
        String url = "https://newcar.xcar.com.cn/";
        String site_biz = site + "-realtime";//实时
        HttpConfig httpConfig = HttpConfig.me(domain);
        httpConfig.setHttpSite(site);

        CrawlerRequestRecord record = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .httpConfig(httpConfig)
                .httpUrl(url)
                .proxy(proxy)
                .httpHead("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36")
                .domain(domain)
                .releaseTime(System.currentTimeMillis())
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24*1,null))
//                .addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-","filter",domain,site,site_biz,"queue")))
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .build();
        record.tagsCreator().bizTags().addDomain(domain);
        record.tagsCreator().bizTags().addSite(site);
        record.tagsCreator().bizTags().addSiteBiz(site_biz);
        record.getHttpRequest().setResponseCharset("UTF-8");

        CrawlerRecord filterCrawlerRecord =  new CrawlerRecord();
        filterCrawlerRecord.setFilter(CrawlerEnum.CrawlerRecordFilter.keyOrDateRange);
        filterCrawlerRecord.addFilterInfo(FilterUtils.redisFilterKeyInfo(domain));
        filterCrawlerRecord.addFilterInfo(FilterUtils.dateRangeFilterInfo(24*1,null));

        //请求队列名
        String requestQueueName = StringUtils.joinWith("-","crawler",domain,site,site_biz,"queue");
        //车系关键次
        CrawlerRequestRecord initNewsCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("xcar_series",turnPageItem)
                .httpUrl(metaServiceHttpPrefix + "/v1/meta/xcar/keys?site=carSeries")//http://192.168.1.217:9959/v1/meta/xcar/keys?site=carSeries
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        //配置结果队列信息
        String resultQueueName = "crawler_result_queue_XCAR_ARTICLE";
        CrawlerQueueConfig crawlerQueueConfig = new CrawlerQueueConfig(
                resultQueueName,
                CrawlerQueueConfig.Content.result,
                CrawlerQueueConfig.Storage.redis,
                CrawlerQueueConfig.Structure.list);

        //同步到mysql callback record
        CrawlerRequestRecord syncCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain+"_"+site+"_sync", turnPageItem)
                .httpUrl(metaServiceHttpPrefix + "/crawler/domain/common/api/v1/"+domain+"/search/results/sync?resultQueue="
                        +resultQueueName + "&site=" + site)
                .requestLabelTag(supportCallback)
                .requestLabelTag(internalDownload)
                .build();

        CrawlerJob crawlerJob = CrawlerJob.builder()
                .crawlerJobThreadNumber(10)
                .triggerInfo(
                        domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),//xcar-SN-realtime-realtime
                        StringUtils.joinWith("-", domain, site,"realtime", CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(requestQueueName))
                .queueResultPipeline("redis",crawlerQueueConfig)
                .kafkaResultPipeline("kafka", kafkaTopciForTraceJob, null)
                .requestRecord(record)
                .supportRecord(initNewsCrawlerRecord)
                .supportRecord(syncCrawlerRecord)
                .build();
        return crawlerJob;
    }

}
